const fs = require('fs');
const path = require('path');
const babel = require('@babel/core');

const srcDir = 'src';
const libDir = 'lib';

const files = fs.readdirSync(srcDir);

files.forEach(file => {
  const filePath = path.join(srcDir, file);
  const fileExt = path.extname(file);

  // 只处理js文件
  if (fileExt === '.js') {
    const code = fs.readFileSync(filePath, 'utf-8');

    // 使用Babel转换代码
    const transformedCode = babel.transformSync(code, {
      presets: ['@babel/preset-env']
    }).code;

    // 修改文件名后缀
    const newFileName = file.replace('.js', '.es5.js');

    // 写入转换后的代码到lib文件夹下
    const newFilePath = path.join(libDir, newFileName);
    fs.writeFileSync(newFilePath, transformedCode);
  }
});